﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System.Windows.Threading;
using GameLogic.Controls;

namespace GameLogic.Controls
{
    [TemplatePart(Name = C_LivesTextName, Type = typeof(TextBlock))]
    public class LifeControl : Control
    {
        const string C_LivesTextName = "_livesText"; // Name of the TextBlock displaying the lives
        TextBlock livesText; // TextBlock displaying the lives
        int currentValue; // Current amount of lives

        // Ammount of lives at game start
        [Category("LifeControl")]
        public int StartValue
        {
            get { return (int)GetValue(StartValueProperty); }
            set { SetValue(StartValueProperty, value); }
        }

        public static readonly DependencyProperty StartValueProperty =
            DependencyProperty.Register(
                "StartValue",
                typeof(int),
                typeof(LifeControl),
                new PropertyMetadata(3, null));

        // Current amount of lives
        public int CurrentValue
        {
            get { return currentValue; }
        }


        // Constructor
        public LifeControl()
        {
            this.DefaultStyleKey = typeof(LifeControl);
            currentValue = this.StartValue;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            livesText = GetTemplateChild(C_LivesTextName) as TextBlock;
            this.UpdateDisplay();
        }

        // Writes current amount of lives to livesText and updates the layout
        void UpdateDisplay()
        {
            // only update if visible
            if (livesText != null)
            {
                livesText.Text = this.currentValue.ToString();
                UpdateLayout();
            }
        }

        /// <summary>
        /// Add value to current lives (negative value to decrease lives)
        /// </summary>
        public void Add(int value)
        {
            currentValue += value;
            this.UpdateDisplay();
            this.OnLivesChanged(value);
        }

        /// <summary>
        /// Initialize lives
        /// </summary>
        public void Initialize()
        {
            currentValue = this.StartValue;
            this.UpdateDisplay();
        }

        /// <summary>
        /// Fired when lives were changed
        /// </summary>
        public event EventHandler<LivesChangedEventArgs> LivesChanged;
        void OnLivesChanged(int value)
        {
            if (this.LivesChanged != null)
            {
                LivesChangedEventArgs args = new LivesChangedEventArgs();
                args.Lives = value;
                this.LivesChanged(this, args);
            }

            if (this.currentValue <= 0)
            {
                this.OnLivesDefeat();
            }
        }

        /// <summary>
        /// Fired when all lives are gone
        /// </summary>
        public event EventHandler LivesDefeat;
        void OnLivesDefeat()
        {
            if (this.LivesDefeat != null)
            {
                this.LivesDefeat(this, EventArgs.Empty);
            }
        }

    }
}
